<%@include file="common/common.jsp" %>
<%
String rkey = (request.getParameter("key"));
String rtype = request.getParameter("type");
String rhkey = (request.getParameter("hkey"));
String rindex = request.getParameter("index");
String rvalue = (request.getParameter("value"));
String oldvalue = (request.getParameter("oldvalue"));
String rscore = request.getParameter("score");
String value = null;

%>
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>

<meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
<meta name=format-detection content="telephone=no">

<meta name=robots content="noindex,nofollow,noarchive">

<meta name=author content="https://github.com/ErikDubbelboer/">

<title></title>

<link rel=stylesheet href="css/common.css?v1-1-1" media=all>
<link rel=stylesheet href="css/frame.css?v1-1-1" media=all>

<link rel="shortcut icon" href="images/favicon.png">

<script src="js/jquery.js?v1-1-1"></script>
<script src="js/frame.js?v1-1-1"></script>
<%
//edit
if(oldvalue != null && rvalue != null) {
	if(rkey != null) {
		if("hash".equals(rtype) && rhkey != null && !"null".equals(rhkey)) {
			jedis.hset(rkey, rhkey, rvalue);
		} else if("list".equals(rtype) && rindex != null && !"null".equals(rindex)) {
			long lsize = jedis.llen(rkey);
			if(isNull(rindex) || Long.parseLong(rindex) == lsize || Long.parseLong(rindex) == -1) {
				jedis.rpush(rkey,rvalue);
			} else if(Long.parseLong(rindex) >= 0 || Long.parseLong(rindex) < lsize){
				jedis.lset(rkey, Long.parseLong(rindex), rvalue);
			} else {
				// null
			}
		} else if("set".equals(rtype) && rvalue != null && !"null".equals(rvalue)) {
			jedis.sadd(rkey, rvalue);
		} else if("zset".equals(rtype) && rvalue != null && !"null".equals(rvalue)) {
			jedis.zadd(rkey, Double.parseDouble(rscore), rvalue);
		} else if("string".equals(rtype)){
			jedis.set(rkey, rvalue);
		}
	}
%>
<script>
  top.location.href = top.location.pathname+'?view&s='+"<%=selectServer%>"+'&key='+"<%=haiyaoyige(rkey)%>";
  </script>
 <%} 
boolean edit=true;
if(rkey != null) {
	if("hash".equals(rtype) && !isNull(rhkey)) {
		value = jedis.hget(rkey, rhkey);
	} else if("list".equals(rtype) && !isNull(rindex)) {
		if(Long.parseLong(rindex)<jedis.llen(rkey))
			value = jedis.lindex(rkey, Long.parseLong(rindex));
	} else if("set".equals(rtype) && !isNull(rvalue)) {
		value = rvalue;
	} else if("zset".equals(rtype) && !isNull(rvalue)) {
		value = rvalue;
	} else if("string".equals(rtype)){
		value = jedis.get(rkey);
	} else {
		edit = false ;
	}
} else {
	edit = false ;
}
 
 %>
</head>
<body>
<h2><%=edit?"EDIT":"ADD" %></h2>
<form action="edit.jsp" method="post">
<input type="hidden" name="s" value="<%=selectServer%>">
<p>
<label for="type">Type:</label>
<select name="type" id="type">
<option value="string" <%=!"string".equals(rtype)?"":"selected='selected'"%>>String</option>
<option value="hash" <%=!"hash".equals(rtype)?"":"selected='selected'"%>>Hash</option>
<option value="list" <%=!"list".equals(rtype)?"":"selected='selected'"%>>List</option>
<option value="set"  <%=!"set".equals(rtype)?"":"selected='selected'"%>>Set</option>
<option value="zset" <%=!"zset".equals(rtype)?"":"selected='selected'"%>>ZSet</option>
</select>
</p>

<p>
<label for="key">Key:</label>
<input type="text" name="key" id="key" size="30" maxlength="30" value="<%=rkey==null?"":haiyaoyige(rkey) %>">
</p>

<p id="hkeyp">
<label for="khey">Hash key:</label>
<input type="text" name="hkey" id="hkey" size="30" maxlength="30" value="<%=rhkey==null?"":haiyaoyige(rhkey)%>">
</p>

<p id="indexp">
<label for="index">Index:</label>
<input type="text" name="index" id="index" size="30" value="<%=rindex==null?"":rindex%>"> <span class="info">empty to append, -1 to prepend</span>
</p>

<p id="scorep">
<label for="score">Score:</label>
<input type="text" name="score" id="score" size="30" value="<%=rscore==null?"":rscore%>">
</p>

<p>
<label for="value">Value:</label>
<textarea name="value" id="value" cols="80" rows="20"><%=value==null?"":haiyaoyige(value) %></textarea>
</p>

<input type="hidden" name="oldvalue" value="<%=value==null?"":haiyaoyige(value) %>">

<p>
<input type="submit" class="button" value="<%=edit?"EDIT":"ADD" %>">
</p>

</form>

</body></html>